% update on 6/10
% change log
%% update to 2.3
%% if needed to accelerate calculation
%% try to move findPIE to for m = 1:m_max loop 
% function Qim = elementQ(S, i, m)
function Qim = elementQ(S, i, PIE)
    % 返回Q(i, m)
    %st = ftimer();
    global P J 
    % >>> 6/11 将P J PIE设为全局变量
    s_row = S(i, :); % 储存 S的第i行
    % PIE = findPIE(m, s_row);
    % PIE = zeros((m+1)(m+2)/2,J)  
    % pi（j）的所有排列组合 
    pie_size = size(PIE, 1);
    Qim = 0;
    for ii = 1 : pie_size
        cum_product = 1;
        for jj = 1 : J % 连乘
            cum_product = cum_product * nchoosek(s_row(jj), PIE(ii,jj)) * P(jj)^PIE(ii,jj)*(1-P(jj))^(s_row(jj)-PIE(ii,jj));
        end
        Qim = Qim + cum_product;
    end
    %global T_elementQ N_elementQ
    %T_elementQ = T_elementQ + ftimer()-st;
    %N_elementQ = N_elementQ + 1;
    return;


